Ext.define('console.evalManageWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'console.store.deptStore',
        'console.store.deptGroupStore',
        'console.store.evalMasterDataStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'evalManageWindow',
    init: function () {
        this.launcher = {
            text: '评价管理',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('evalManageWindow');
        if (!win) {
            win = desktop.createWindow({
                id: 'evalManageWindow',
                title: '评价管理',
                width: 780,
                height: 500,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [
                    {
                        border: false,
                        title: '部门分组列表',
                        xtype: 'grid',
                        split: true,
                        animCollapse: false,
                        constrainHeader: true,
                        layout: 'fit',
                        itemId: 'master',
                        store: Ext.createWidget('evalMasterDataStore'),
                        columns: [{xtype: 'rownumberer'},
                            {
                                text: "月份",
                                sortable: true,
                                dataIndex: 'month'
                            }, {
                                text: "科室/部门类别代码",
                                sortable: true,
                                hidden: true,
                                dataIndex: 'groupCode'
                            }, {
                                text: "科室/部门类别",
                                sortable: true,
                                dataIndex: 'groupName'
                            },
                            {
                                text: "科室/部门",
                                sortable: true,
                                dataIndex: 'deptName'
                            },
                            {
                                text: "已填数量",
                                sortable: true,
                                dataIndex: 'fillCount'
                            },
                            {
                                text: "该填数量",
                                sortable: true,
                                dataIndex: 'shouldFillCount'
                            },
                            {
                                text: "状态",
                                sortable: true,
                                flex: 1,
                                dataIndex: 'status',
                                renderer: function (val) {
                                    if (val == "0") {
                                        return "未提交";
                                    } else if (val == "1") {
                                        return "已提交";
                                    } else {
                                        return "未生成评价数据"
                                    }
                                }
                            }
                        ]
                    }
                ],
                tbar: [{
                    text: '生成本月的填报表',
                    tooltip: '添加评价分组',
                    iconCls: 'add',
                    handler: this.add
                }, '-', {
                    text: '刷新',
                    tooltip: '刷新数据',
                    iconCls: 'refresh',
                    handler: this.refresh
                }]
            });
        }
        return win;
    },


    add: function () {

        Ext.MessageBox.confirm('', '您确定要生成本月的评价数据吗?', function (opt) {
            if (opt == 'yes') {

                Ext.create('Ext.window.Window', {
                    modal: true,
                    title:'请填入有效填报的时间段',
                    items: [{
                        xtype: 'form',
                        layout: {
                            type: 'table',
                            columns: 1
                        },
                        defaults: {
                            labelAlign: 'left',
                            margin: '10 10 10 10',
                            width: '90%',
                            xtype: 'textfield'
                        },
                        items: [{
                            name: 'beginTime',
                            xtype: 'datefield',
                            fieldLabel: '开始日期',
                            format: 'Y-m-d',
                            allowBlank: false
                        }, {
                            name: 'endTime',
                            xtype: 'datefield',
                            fieldLabel: '结束日期',
                            format: 'Y-m-d',
                            allowBlank: false
                        }],
                        buttons: [{
                            text: '重置',
                            handler: function () {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            handler: function () {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        url: 'eval/generatorNextMonthData',
                                        timeout: 100000000,//Fun_hanxn190403_add 解决生成本月报表的超时问题
                                        success: function (form, action) {
                                            Ext.Msg.alert('', '操作成功', function () {
                                                form.owner.ownerCt.close();
                                                Ext.getCmp("evalManageWindow").ownerCt.ownerCt.query('grid')[0].store.reload();
                                            }, this);
                                        },
                                        failure: function (form, action) {
                                            var res= Ext.decode(action.response.responseText);
                                            Ext.Msg.alert('操作失败', res.failure);
                                        }
                                    });
                                }
                            }
                        }]
                    }]
                }).show();
            }
        });

    },

    refresh: function () {
        this.ownerCt.ownerCt.query('grid')[0].store.reload();
    }
});

